package com.ruohi.manage;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class PoolManager {
	
	private static DataSource serviceDataSource = null;
	private static DataSource logDataSource = null;
	private static DataSource businessDataSource = null;

	static {
		try {
			Context context = new InitialContext();
			serviceDataSource = (DataSource) context
					.lookup("java:comp/env/service");
			logDataSource = (DataSource) context
					.lookup("java:comp/env/log");
			businessDataSource = (DataSource) context
					.lookup("java:comp/env/business");
			
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection(DBType dbType) throws SQLException {
		if (dbType == DBType.LOG) {
			return logDataSource.getConnection();
		} else if (dbType == DBType.SERVICE) {
			return serviceDataSource.getConnection();
		} else if (dbType == DBType.BUSINESS) {
			return businessDataSource.getConnection();
		} 
		return null;
	}
}